<?php


/**
 * Description of Category
 *
 * @author Admin
 */
class Category_Model_Category extends WOW_Model_Acl_Abstract
{
    
    public function getResourceId()
    {
        return 'Category';
    }

    public function getCategoryById($id)
    {
        $id = (int) $id;
        return $this->getResource('Category')->getCategoryById($id);
    }

    public function getCategoryByIdent($ident)
    {
        return $this->getResource('Category')->getCategoryByIdent($ident);
    }

    public function getAllCategories()
    {
         return $this->getResource('Category')->getAllCategories();
    }

    public function deleteCategory($category)
    {
         if ($category instanceof Category_Resource_Category_Item_Interface) {
            $catid = (int) $category->catid;
        } else {
           $catid = (int) $category;
        }

        $category = $this->getCategoryById($catid);

        if (null !== $category) {
            $category->delete();
            return true;
        }
        else
             throw new WOW_Exception('Unknown category');

        return false;
    }

    public function saveCategory($post)
    {
        $form = $this->getForm('CategoryCategoryform');
        return $this->_save($form, $post);
    }


    protected function _save(Zend_Form $form, array $info, $defaults=array())
    {
        if (!$form->isValid($info))
        {
            return false;
        }

        $data = $form->getValues();
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }

        $data['CategoryName'] = ucwords($data['CategoryName']);
        $ident = $data['CategoryName'];
        $data['catident']   = str_replace(' ', '-', strtolower($ident));

        if($data['catid'] !='')
        {
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        //echo '<br> <pre>'; print_r($data); echo '</pre>';die();
        $category = ($data['catid'] !='') ? $this->getResource('Category')->getCategoryById($data['catid']) : null;

        return $this->getResource('Category')->saveRow($data, $category);
    }

    public function getAllCategoryArray()
    {
        $allcat  = $this->getResource('Category')->getAllCategories();
        $ret_ar = array(' ' => 'Select Category');
        foreach($allcat as $cat)
        {
            $ret_ar[$cat->catid] = $cat->CategoryName;
        }

        return $ret_ar;
    }

    #@ TODO:: needs to be worked on ACL later
    public function setAcl(WOW_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)->allow('Normal', $this, array('register'))
            ->allow('Moderator', $this, array('saveCategory'))
                    ->allow('Admin', $this);
        }
        $this->_acl = $acl;
        return $this;
    }

    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->setAcl(new Main_Model_Acl_Main());
        }
        return $this->_acl;
    }

}
?>
